<template>
  <!--更多操作-->
  <el-popover v-if="checkPermission(permission.moreOpe == undefined ? [] : permission.moreOpe)" placement="bottom" width="100" trigger="click">
    <img slot="reference" class="img" src="../../assets/images/more.png" alt="">
    <el-row>
      <el-button
        v-permission="permission.edit"
        type="text"
        :disabled="crud.selections.length !== 1"
        @click="crud.toEdit(crud.selections[0])"
      >修改</el-button>
    </el-row>
    <el-row>
      <el-button
        v-permission="permission.del"
        :disabled="crud.selections.length == 0"
        type="text"
        :loading="crud.delAllLoading"
        @click="toDelete(crud.selections)"
      >删除</el-button>
    </el-row>
    <el-row>
      <el-button
        v-permission="permission.enable"
        :disabled="crud.selections.length == 0"
        type="text"
        :loading="crud.delAllLoading"
        @click="toEnable(crud.selections)"
      >启用</el-button>
    </el-row>
    <el-row>
      <el-button
        v-permission="permission.disable"
        :disabled="crud.selections.length == 0"
        type="text"
        :loading="crud.delAllLoading"
        @click="toDisable(crud.selections)"
      >停用</el-button>
    </el-row>
  </el-popover>
</template>
<script>
import CRUD, { crud, pagination } from '@crud/crud'
import checkPermission from '@/utils/permission' // 权限判断函数

export default {
  mixins: [crud({ type: 'operation' }), pagination()],
  components: { checkPermission },
  props: {
    permission: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      allColumnsSelected: true,
      allColumnsSelectedIndeterminate: false
    }
  },
  created() {

  },
  methods: {
    checkPermission,
    toDelete(datas) {
      this.$confirm(`确认删除选中的${datas.length}条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.crud.delAllLoading = true
        if (datas instanceof Array && datas.length > 0) {
          // 删除的两种类型  pkId(常用删除)和id(用户sys_user删除)
          if (datas[0].hasOwnProperty('id')) {
            this.crud.doDelete(datas)
          } else {
            this.crud.doDeleteByPkId(datas)
          }
        }
      }).catch(() => {
      })
    },
    toEnable(datas) {
      this.$confirm(`确认启用选中的${datas.length}条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.crud.delAllLoading = true
        if (datas instanceof Array && datas.length > 0) {
          this.crud.doEnabelByPkId(datas)
        }
      }).catch(() => {
      })
    },
    toDisable(datas) {
      this.$confirm(`确认停用选中的${datas.length}条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.crud.delAllLoading = true
        if (datas instanceof Array && datas.length > 0) {
          this.crud.doDisabelByPkId(datas)
        }
      }).catch(() => {
      })
    },
    handleCheckAllChange(val) {
      if (val === false) {
        this.allColumnsSelected = true
        return
      }
      for (const key in this.crud.props.tableColumns) {
        this.crud.props.tableColumns[key].visible = val
      }
      this.allColumnsSelected = val
      this.allColumnsSelectedIndeterminate = false
    },
    handleCheckedTableColumnsChange(item) {
      let totalCount = 0
      let selectedCount = 0
      for (const key in this.crud.props.tableColumns) {
        ++totalCount
        selectedCount += this.crud.props.tableColumns[key].visible ? 1 : 0
      }
      if (selectedCount === 0) {
        this.crud.notify('请至少选择一列', CRUD.NOTIFICATION_TYPE.WARNING)
        this.$nextTick(function() {
          item.visible = true
        })
        return
      }
      this.allColumnsSelected = selectedCount === totalCount
      this.allColumnsSelectedIndeterminate = selectedCount !== totalCount && selectedCount !== 0
    }
  }
}
</script>
